package io.renren.modules.gengyun.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.gengyun.dao.GatherTimeDao;
import io.renren.modules.gengyun.entity.Date_Api;
import io.renren.modules.gengyun.entity.IndicatorInfoEntity;
import io.renren.modules.gengyun.entity.spare.IndicatorShow;
import io.renren.modules.gengyun.service.GatherTimeService;
import io.renren.modules.gengyun.service.IndicatorPeriodService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.gengyun.entity.IndicatorEntity;
import io.renren.modules.gengyun.service.IndicatorService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author krlin
 * @email 2032153705@qq.com
 * @date 2020-09-17 20:31:48
 */
@Api("参数指标情况获取接口")
@RestController
@RequestMapping("gengyun/indicator")
public class IndicatorController {
    @Autowired
    private IndicatorService indicatorService;
    @Autowired
    private IndicatorPeriodService indicatorPeriodService;
    @Autowired
    private GatherTimeService gatherTimeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("gengyun:indicator:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = indicatorService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gengyun:indicator:info")
    public R info(@PathVariable("id") Long id){
		IndicatorEntity indicator = indicatorService.getById(id);

        return R.ok().put("indicator", indicator);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gengyun:indicator:save")
    public R save(@RequestBody IndicatorEntity indicator){
		indicatorService.save(indicator);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gengyun:indicator:update")
    public R update(@RequestBody IndicatorEntity indicator){
		indicatorService.updateById(indicator);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("gengyun:indicator:delete")
    public R delete(@RequestBody Long[] ids){
		indicatorService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


//    /**
//     * 查询某一地块的各种参数值
//     * @param params
//     * @return
//     */
//    @RequestMapping("/listIndicators")
//    @RequiresPermissions("gengyun:indicator:list")
//    public R listLatestIndicators(@RequestParam Map<String, Object> params) {
//        String cropRotationId = (String) params.get("cropRotationId");
//        List<IndicatorShow> indicatorInfos = indicatorService.indicatorShows(cropRotationId);
//        Map<String, Double> m = indicatorService.getCycleOfIndicatorsByCropRotationId(cropRotationId,
//                null,
//                null);
//        indicatorInfos.forEach( x-> {
//
//            x.setCycleOfIndicators(m.get(x.getIndicatorId()));
//            if (x.getIndicatorCoefficient() != null) {
//                Double a = x.getDigitValue() / Math.pow(10, x.getIndicatorCoefficient());
//                x.setDigitValue(a);
//            }
//        });
//        return R.ok().put("indicatorInfos", indicatorInfos);
//    }


    /**
     * 查询某一地块,某一指标的变化
     * @param params
     * @return
     */
//    @RequestMapping("/listIndicatorByCropRotationId")
//    @RequiresPermissions("gengyun:indicator:list")
//    public R listIndicatorsByCropRotationId(@RequestParam Map<String, Object> params) {
//        QueryWrapper<IndicatorEntity> wrapper = new QueryWrapper<>();
//        String cropRotationId = (String) params.get("cropRotationId");
//        String indicatorId = (String) params.get("indicatorId");
//        String beginTime = (String) params.get("beginTime");
//        String endTime = (String) params.get("endTime");
//        /**
//         * 指标基本情况
//         */
//        List<IndicatorShow> indicatorInfos = indicatorService.findByIndicatorIdAndCropRotationId(
//                cropRotationId,
//                indicatorId,
//                Long.parseLong(beginTime),
//                Long.parseLong(endTime)
//        );
//
//        Map<String, Double> m = indicatorService.getCycleOfIndicatorsByCropRotationId(cropRotationId,
//                Long.parseLong(beginTime),
//                Long.parseLong(endTime));
//
//        indicatorInfos.forEach( x-> {
//            x.setCycleOfIndicators(m.get(x.getIndicatorId()));
//            if (x.getIndicatorCoefficient() != null) {
//                Double a = x.getDigitValue() / Math.pow(10, x.getIndicatorCoefficient());
//                x.setDigitValue(a);
//            }
//        });
//        return R.ok().put("indicatorInfos", indicatorInfos);
//    }

//    public R getCycleOfIndicatorsByCropRotationId(@RequestParam Map<String, Object> params) {
//        String cropRotationId = (String) params.get("cropRotationId");
//        String indicatorId = (String) params.get("indicatorId");
//    }


    @RequestMapping("/getIndicatorsByTime")
    @RequiresPermissions("gengyun:indicator:list")
    public R getIndicatorsByTime(@RequestParam Map<String, Object> params) {
//        QueryWrapper<IndicatorEntity> wrapper = new QueryWrapper<>();
        String cropRotationId = (String) params.get("cropRotationId");
        String time = (String) params.get("time");
        String indicatorType = (String) params.get("indicatorType");
        HashMap<String,String> period = Date_Api.initData(time);
//        String beginTime = (String) params.get("time");
//        System.out.println(time);
//        String endTime = (String) params.get("endTime");
        String beginTime = period.get("beginTime");
        String endTime = period.get("endTime");
        System.out.println("string = " + beginTime + " long = " + endTime);
        List<Map> indicatorsByTime = indicatorService.getIndicatorsByTime(cropRotationId,
                indicatorType,
                Long.parseLong(beginTime),
                Long.parseLong(endTime));

        return R.ok().put("indicatorsByTime",indicatorsByTime);
//        return R.ok().put("indicatorsByTime",null);
    }

    @RequestMapping("/CompareIndicator")
    @RequiresPermissions("gengyun:indicator:list")
    public R CompareIndicatorIdAndCropRotationId(@RequestParam Map<String, Object> params) throws ParseException {
        String cropRotationId = (String) params.get("cropRotationId");
        String indicatorId = (String) params.get("indicatorId");
        String beginTimeTemp = (String) params.get("compareYears");

        HashMap<String,String> timesHashMap = Date_Api.getCompareDates(beginTimeTemp);
        String beginTime = timesHashMap.get("beginTime");
        String endTime = timesHashMap.get("endTime");
        List<Map<String,Object>> stringListMap = indicatorService.compareAllYearsDataByTargetName(cropRotationId,
                indicatorId,
                Long.parseLong(beginTime),
                Long.parseLong(endTime));

        int length = stringListMap.size();
//        HashMap<String,Object> mapTemp = new HashMap<>();
        for (Map<String, Object> map: stringListMap){
//            mapTemp.put(map.get("format_time").toString().substring(0,4),map.get("format_time").toString().substring(0,4));
            map.put("format_year",map.get("format_time").toString().substring(0,4));
        }
        Map<String,Object> map = new HashMap<>();
//        Map<String,Object> returnMap = new HashMap<>();
        List<Map<String,Object>> returnMapList = new ArrayList<>();
        if (!stringListMap.isEmpty()) {
            String nowTime = stringListMap.get(0).get("format_year").toString();
            List<String> valueArrayList = new ArrayList<>();
            List<String> dateArrayList = new ArrayList<>();
            for (int i = 0; i < length - 1; i++) {
                if (nowTime.equals(stringListMap.get(i).get("format_year"))) {
                    valueArrayList.add(stringListMap.get(i).get("digit_value").toString());
                    dateArrayList.add(stringListMap.get(i).get("format_time").toString().substring(0,10));
                } else {
                    map.put("time", nowTime);
                    map.put("value", valueArrayList);
                    map.put("dates", dateArrayList);
                    returnMapList.add(map);
                    System.out.println("add + " + nowTime + valueArrayList.size());
                    nowTime = stringListMap.get(i).get("format_year").toString();
                    map = new HashMap<>();
                    valueArrayList = new ArrayList<>();
                    dateArrayList = new ArrayList<>();
                }

            }
            dateArrayList.add(stringListMap.get(length - 1).get("format_time").toString());
            valueArrayList.add(stringListMap.get(length - 1).get("digit_value").toString());
            map.put("time", nowTime);
            map.put("value", valueArrayList);
            map.put("dates", dateArrayList);
            returnMapList.add(map);
//            List<String> periods = gatherTimeService.getTimesByIndicator("土壤10cmEC");
//            returnMap.put("times",periods);
//            returnMap.put("list",returnMapList);

        }
        return R.ok().put("stringListMap",returnMapList);
    }

    @RequestMapping("/getCropRotationId")
    @RequiresPermissions("gengyun:indicator:list")
    public R getCropRotationId() {
//        Map<String, List<IndicatorShow>> stringListMap = indicatorService.;
        List<String> stringList = indicatorService.getCropRotationId();
        return R.ok().put("stringListMap",stringList);
    }

    @RequestMapping("/getIndicatorTarget")
    @RequiresPermissions("gengyun:indicator:list")
    public R getIndicatorTarget(@RequestParam Map<String,Object> params){
//        Map<String, List<IndicatorShow>> stringListMap = indicatorService.;
        String target = (String) params.get("target");

//        System.out.println("========================="+target);
        List<String> stringList = indicatorPeriodService.getIndicators(target);
        return R.ok().put("stringListMap",stringList);
    }

    @RequestMapping("/getProductIndicator")
    @RequiresPermissions("gengyun:indicator:list")
    public R getProductIndicator(@RequestParam Map<String,Object> params){
        String cropRotationId = (String) params.get("cropRotationId");
        String time = (String) params.get("time");
        String indicatorType = (String) params.get("indicatorType");
        HashMap<String,String> period = Date_Api.initData(time);
        String beginTime = period.get("beginTime");
        String endTime = period.get("endTime");
        List<Map> indicatorsByTime = indicatorService.getIndicatorsByTime(cropRotationId,
                indicatorType,
                Long.parseLong(beginTime),
                Long.parseLong(endTime));
        return R.ok().put("indicatorsByTime",indicatorsByTime);
    }
//    Map<Sting , IndicatorShow>



}
